<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String imgSrc=basePath+"resources/images/noImage.jpg";
    session.setAttribute("imgSrc",imgSrc);
%>
<!DOCTYPE html>
<html style="background-color: #fff;">
<head>
    <%@include file="../../../resources/inc/head.jsp" %>
</head>
<body>

<div class="layui-form" lay-filter="layuiadmin-form-permissionUser" id="layuiadmin-form-permissionUser" style="padding: 20px;">
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">

        <div class="layui-upload">
            <input type="hidden" value="${permissionUserTo.avatar==null?imgSrc:permissionUserTo.avatar}" name="avatar" id="avatar" lay-verify="required" class="layui-input">
        <div class="layui-upload-list">
            <img class="layui-upload-img" width="100px" src="${permissionUserTo.avatar==null?imgSrc:permissionUserTo.avatar}" id="avatarImg">
        </div>
            <button type="button" class="layui-btn" id="avatarBth"><i class="layui-icon layui-icon-upload"></i>上传图片</button>

        </div>
        </div>
    </div>
    <div class="layui-form-item">
        <input type="hidden" value="${permissionUserTo.id}" name="id">
        <label class="layui-form-label">用户名称</label>
        <div class="layui-input-block">
            <input type="text" value="${permissionUserTo.userName}" name="userName" lay-verify="required" placeholder="请输入用户名称"
                   autocomplete="off" class="layui-input">
        </div>
    </div>
    <c:if test="${permissionUserTo.password==null}">
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
    </c:if>
    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名</label>
        <div class="layui-input-block">
            <input type="text" value="${permissionUserTo.realName}" name="realName" lay-verify="required" placeholder="请输入真实姓名" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机号码</label>
        <div class="layui-input-block">
            <input type="text" value="${permissionUserTo.phone}" name="phone"  lay-verify="required" placeholder="请输入手机号码" autocomplete="off"
                   class="layui-input">
        </div>
    </div>


    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="LAY-submit" id="LAY-submit" value="确认">
    </div>
</div>

<%@include file="../../../resources/inc/footer.jsp" %>

<script>
    layui.config({
        base: '<%=basePath%>resources/lib/layuiAdmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'upload'], function () {
        var $ = layui.$
            , form = layui.form
            , upload = layui.upload;


            var avatarUploaderInst = upload.render({
                elem: '#avatarBth'
                , url: '<%=basePath%>/manager/file/upload.action?catalog=image&fileType=image&sizeLimit=1&oss=alioss'
                , before: function (obj) {
                    obj.preview(function (index, file, result) {
                        $('#avatarImg').attr('src', result);
                    });
                }
                , done: function (res) {
                    if (res.success) {
                        $("#avatar").val(res.url);
                        return layer.msg('上传成功');
                    }else{
                        return layer.msg(res.message);
                    }
                }
                , error: function () {
                    layer.msg("上传失败!");
                }
            });
    });
    function createSubmit() {
        var layer=layui.layer;
        var submitID = 'LAY-submit';
        layui.form.on('submit('+ submitID +')', function(data){
            var formData = data.field;
            var b = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/g;

            layer.load(2);

            if(formData.id==null||formData.id==""){
                if(!b.test(formData.password)){
                    layer.msg("密码必须包含数字与字母,且必须为八到十六位.")

                    return;
                }
                formData.id = null;
                $.ajax({
                    url:"<%=basePath%>manager/permission/user/create.action",
                    type:"POST",
                    data:formData,
                    dataType:"JSON",
                    success:function (data) {
                        layer.closeAll('loading');
                        if(data.success){
                            layer.msg("创建成功,1秒后跳转")
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            },1000);
                        }else{
                            layer.msg(data.msg, {icon: 5});
                        }
                    }
                });
            }else{

                $.ajax({
                    url: "<%=basePath%>manager/permission/user/update.action",
                    type:"POST",
                    data:formData,
                    dataType:"JSON",
                    success:function (data) {
                        layer.closeAll('loading');
                        if(data.success){
                            layer.msg("修改成功,1秒后跳转");
                            setTimeout(function () {
                                window.parent.location.reload(); //刷新父页面
                                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                                parent.layer.close(index);
                            },1000);
                        }else{
                            layer.msg(data.msg, {icon: 5});
                        }
                    }
                });
            }
        })
        $("#"+submitID).trigger('click');
    }
</script>
</body>
</html>
